import numpy as np
import matplotlib.pyplot as plt


# region 三次Bezier曲线
def operate_Bezier(i):
    t = i * 1.0
    t2 = t * t
    t3 = t2 * t

    a1 = -1 * t3 + 3 * t2 - 3 * t + 1
    a2 = 3 * t3 - 6 * t2 + 3 * t
    a3 = -3 * t3 + 3 * t2
    a4 = t3

    x = a1 * p1[0] + a2 * p2[0] + a3 * p3[0] + a4 * p4[0]
    y = a1 * p1[1] + a2 * p2[1] + a3 * p3[1] + a4 * p4[1]
    return [x, y]


def compute_Bezier():
    t_values = np.linspace(0, 1, 500)
    items = []
    for i in t_values:
        items.append(operate_Bezier(i))
    return items


# endregion


if __name__ == '__main__':
    # 创建一个全为0的二维数组
    img = np.zeros([501, 501], dtype=np.uint8)
    # 填充255表示白色
    img.fill(255)

    p1 = [100, 100]
    p2 = [200, 300]
    p3 = [400, 300]
    p4 = [300, 100]
    # print(matrix)
    items1 = compute_Bezier()
    for i in items1:
        x = int(i[0])
        y = int(i[1])
        img[x, y] = 0

    # 由于imshow默认的坐标系是左上角为原点，所以需要转换一下
    img = np.transpose(img)
    plt.title('Bezier')
    plt.imshow(img, cmap='gray', origin='lower')
    # 显示图片
    plt.show()
